
#include <cmath>
#include <iostream>

#include "Heuristics.hpp"

int Heuristics::manhattan(Puzzle const & p, Puzzle const & solution)
{
    int total = 0;
    if (p.getSize() == solution.getSize())
    {
        Coord c;
        int max = sqrt(p.getSize());
        for (c.x = 0; c.x < max; c.x++)
        {
            for (c.y = 0; c.y < max;  c.y++)
            {
                int r = Heuristics::_getManhattanDistance(p, c, solution.getAt(c));
                total += r;
                // std::cout << p.getAt(c) << " - " << solution.getAt(c) << " : " << r << std::endl;
            }
        }
    }
    // std::cout << total << std::endl;
    return total;
}

int Heuristics::_getManhattanDistance(Puzzle const & p, Coord const & c, int value)
{
    for (int x = 0; x < p.getSize(); x++)
    {
        for (int y = 0; y < p.getSize(); y++)
        {
            if (p.getAt(x, y) == value)
                return ABS(c.x - x) + ABS(c.y - y);
        }
    }
    return 1000;
}
